<template>
  <div id="nav-bar">
    <div class="search">
      <input type="text" v-model="title" placeholder="搜索文章..."/>
      <span :style="'background-image: url(' + bgImg + ')'" @click="submit"></span>
    </div>
    <span class="login">
      <span v-show="!isLogin" @click="loginClick">登录</span>
      <span v-show="isLogin" @click="enterClick">进入后台</span>
    </span>
  </div>
</template>

<script>
import bgImg from "@/assets/icon/fa-search.png"

export default {
  name: "NavBar",
  data() {
    return {
      title: "",
      isLogin: this.$store.getters.isLogin,
      bgImg
    }
  },
  methods: {
    submit() {
      this.$bus.$emit('search', this.title); //事件总线发送事件
    },
    loginClick() {
      this.$router.push("/login").catch(err => err);
    },
    enterClick() {
      this.$router.push("/user/blog/edit").catch(err => err);
    }
  },
  destroyed() {
    this.$bus.$off("search");
  }
  // ,
  // created() {
  //   console.log(this.$store.getters.isLogin)
  // }
}
</script>

<style scoped>
  #nav-bar {
    width: 100%;
    height: 5%;
    background-color: rgba(255, 255, 255, 0.9);
    border: 1px solid #d5d5d5;
    box-shadow: 5px 5px 5px 0 rgba(0, 0, 0, 0.3);
  }

  #nav-bar .search {
    position: absolute;
    top: 50%;
    left: 50%;
    height: 100%;
    margin-left: -194px;
    margin-top: -21px;
  }

  #nav-bar .search input {
    float: left;
    width: 346px;
    height: 40px;
    padding-left: 15px;
    border: 1px solid #00b7ff;
    border-right: 0;
    font-size: 14px;
  }

  #nav-bar .search span {
    float: left;
    width: 42px;
    height: 42px;
    background-color: #00a4ff;
    background-repeat: no-repeat;
    background-position: center;
  }

  #nav-bar .search span:hover {
    background-color: #00B7FF;
    cursor: pointer;
  }

  #nav-bar .login {
    position: absolute;
    top: 50%;
    right: 5%;
    margin-top: -16px;
    width: 95px;
    height: 35px;
    line-height: 35px;
    text-align: center;
    font-size: 15px;
    color: white;
    background-color: #999999;
  }

  #nav-bar .login:hover {
    background-color: darkgrey;
    cursor: pointer;
  }
</style>